//
//  Status.swift
//  weibo
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit

class Status: NSObject {
    //微博创建时间
    var created_at: String?
    //微博ID
    var id: Int64 = 0
    //微博信息内容
    var text: String?
    //微博来源
    var source: String?
    //用户模型
    var user: User?
    //配图的数组
    var pic_urls: [[String : String]]? {
        didSet {
            guard let array = pic_urls else {
                return
            }
            //遍历数组
            //将数组实例化
            imageURLs = [NSURL]()
            for item in array {
                //相信后端 一定能返回一个string类型的url
                var urlString = item["thumbnail_pic"]!
//                urlString = urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "moderate")
                //相信后端给定的urlString 一定能够转换为urlstring
                let url = NSURL(string: urlString)!
                //添加url
                imageURLs!.append(url)
            }
        }
    }
    //将配图数组转换为 url对象
    var imageURLs: [NSURL]?
    //被转发的原微博信息字段，当该微博为转发微博时返回 详细
    var retweeted_status: Status?
    //构造方法 KVC设置值
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            //字典转模型
            if let dict = value as? [String: AnyObject] {
                user = User(dict: dict)
                print(user)
            }
            //需要加return 不然上面字典转模型就白转了
            return
        }
        if key == "retweeted_status" {
            if let dict = value as? [String:AnyObject] {
            retweeted_status = Status(dict: dict)
                return
            }
        }
        //给其他的属性设置值
        super.setValue(value, forKey: key)
    }
    //过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    override var description:String {
        let keys = ["created_at","id","text","source","user"]
        return dictionaryWithValuesForKeys(keys).description
    }
}


